package com.mining.app.zxing.encoding;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.json.JSONTokener;

public class WifiInfo {
	
	private String ssid;
	private String pwd;
	private NetworkType type;
	
	public WifiInfo(String ssid, String pwd, NetworkType type) {
		super();
		this.ssid = ssid;
		this.pwd = pwd;
		this.type = type;
	}


	public WifiInfo() {
		super();
		this.ssid = null;
		this.pwd = null;
		this.type = null;
	}


	public static String wifiInfoToJsonStr(String ssid, String pwd, NetworkType type){
		JSONStringer jsonStr = new JSONStringer();
		// TODO test for null value
		try{			
			jsonStr.object();
			
		    jsonStr.key("ssid");  
		    jsonStr.value(ssid);
		    
		    jsonStr.key("pwd");  
		    jsonStr.value(pwd);
		    
		    jsonStr.key("type");  
		    jsonStr.value(type.toString());
		    
		    jsonStr.endObject();  
		}
		catch (JSONException ex) {   
		    throw new RuntimeException(ex);  
		}
		
		return jsonStr.toString();
	}
	
	public static WifiInfo jsonStrToWifiInfo(String jsonStr) throws JSONException{
		String strSsid = null;
		String strPwd = null;
		NetworkType type = null;	
		WifiInfo retInfo = new WifiInfo();
		
		try {  
		    JSONTokener jsonParser = new JSONTokener(jsonStr);  

		    JSONObject wifiInfo = (JSONObject) jsonParser.nextValue(); 
		    strSsid = wifiInfo.getString("ssid");
		    strPwd = wifiInfo.getString("pwd");
		    type = NetworkType.forIntentValue(wifiInfo.getString("type"));
		    
		    retInfo.setSsid(strSsid);
		    retInfo.setPwd(strPwd);
		    retInfo.setType(type);

		} catch (JSONException ex) {  
			throw ex;  
		}  
		return retInfo;
	}


	public String getSsid() {
		return ssid;
	}


	public void setSsid(String ssid) {
		this.ssid = ssid;
	}


	public String getPwd() {
		return pwd;
	}


	public void setPwd(String pwd) {
		this.pwd = pwd;
	}


	public NetworkType getType() {
		return type;
	}


	public void setType(NetworkType type) {
		this.type = type;
	}

}
